package co.fitstart.admin.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.support.Page;
import co.fitstart.admin.entity.search.OfflineExperienceSignUpSearch;
import co.fitstart.admin.entity.search.OnlineExperienceSignUpSearch;
import co.fitstart.admin.service.ExperienceSignUpService;
import co.fitstart.entity.market.OfflineExperienceSignUp;
import co.fitstart.entity.market.OnlineExperienceSignUp;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Service("experienceSignUpService")
public class ExperienceSignUpServiceImpl implements ExperienceSignUpService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ExperienceSignUpService#listOffline(co.fitstart.entity.site.Site, java.lang.Boolean, com.demo2do.core.support.Page)
     */
    @SuppressWarnings("unchecked")
    public List<OfflineExperienceSignUp> listOffline(Site site, Boolean handle, Page page) {
        OfflineExperienceSignUpSearch search = new OfflineExperienceSignUpSearch();
        search.setSite(site);
        search.setHandled(handle);
        
        String s = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        List<OfflineExperienceSignUp> result = genericDaoSupport.searchForList(s, parameters, page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ExperienceSignUpService#listOffline(java.util.List, java.lang.Boolean, com.demo2do.core.support.Page)
     */
    @SuppressWarnings("unchecked")
    public List<OfflineExperienceSignUp> listOffline(List<Site> sites, Boolean handle, Page page) {
        OfflineExperienceSignUpSearch search = new OfflineExperienceSignUpSearch();
        search.setSites(sites);
        search.setHandled(handle);
        
        String s = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        List<OfflineExperienceSignUp> result = genericDaoSupport.searchForList(s, parameters, page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ExperienceSignUpService#loadOffline(java.lang.Long)
     */
    public OfflineExperienceSignUp loadOffline(Long experienceId) {
        return genericDaoSupport.load(OfflineExperienceSignUp.class, experienceId);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ExperienceSignUpService#handle(co.fitstart.entity.market.OfflineExperienceSignUp, java.lang.String, co.fitstart.entity.principal.Principal)
     */
    public void handle(OfflineExperienceSignUp experience, String remark, Principal handler) {
        genericDaoSupport.update(experience.handle(remark, handler));
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ExperienceSignUpService#search(co.fitstart.admin.entity.search.OfflineExperienceSignUpSearch, com.demo2do.core.support.Page)
     */
    @SuppressWarnings("unchecked")
    public List<OfflineExperienceSignUp> search(OfflineExperienceSignUpSearch search, Page page) {
        String s = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        List<OfflineExperienceSignUp> result = genericDaoSupport.searchForList(s, parameters, page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ExperienceSignUpService#search(co.fitstart.admin.entity.search.OfflineExperienceSignUpSearch)
     */
    @SuppressWarnings("unchecked")
    public List<OfflineExperienceSignUp> search(OfflineExperienceSignUpSearch search) {
        String s = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return genericDaoSupport.searchForList(s, parameters);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ExperienceSignUpService#listOnline(java.lang.Boolean, com.demo2do.core.support.Page)
     */
    @SuppressWarnings("unchecked")
    public List<OnlineExperienceSignUp> listOnline(Boolean handle, Page page) {
        OnlineExperienceSignUpSearch search = new OnlineExperienceSignUpSearch();
        search.setHandled(handle);
        
        String s = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        List<OnlineExperienceSignUp> result = genericDaoSupport.searchForList(s, parameters, page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ExperienceSignUpService#loadOnline(java.lang.Long)
     */
    public OnlineExperienceSignUp loadOnline(Long experienceId) {
        return genericDaoSupport.load(OnlineExperienceSignUp.class, experienceId);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ExperienceSignUpService#handle(co.fitstart.entity.market.OnlineExperienceSignUp, java.lang.String, co.fitstart.entity.principal.Principal)
     */
    public void handle(OnlineExperienceSignUp experience, String remark, Principal handler) {
        genericDaoSupport.update(experience.handle(handler, remark));
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ExperienceSignUpService#search(co.fitstart.admin.entity.search.OnlineExperienceSignUpSearch, com.demo2do.core.support.Page)
     */
    @SuppressWarnings("unchecked")
    public List<OnlineExperienceSignUp> search(OnlineExperienceSignUpSearch search, Page page) {
        String s = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        List<OnlineExperienceSignUp> result = genericDaoSupport.searchForList(s, parameters, page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.ExperienceSignUpService#search(co.fitstart.admin.entity.search.OnlineExperienceSignUpSearch)
     */
    @SuppressWarnings("unchecked")
    public List<OnlineExperienceSignUp> search(OnlineExperienceSignUpSearch search) {
        String s = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return genericDaoSupport.searchForList(s, parameters);
    }

}
